package com.yulinlin.authorization.config;

import com.yulinlin.common.util.RSAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import java.security.KeyPair;


@EnableAuthorizationServer //开启授权服务器的功能
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private AuthenticationManager authenticationManager;


    /**
     * 添加第三方的客户端
     * @param clients
     **/
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()

                //资源服务器
                .withClient("resource") //第三方客户端的名称
                .secret(passwordEncoder.encode("resource"))//第三方客户端的密钥
                .scopes("all") //第三方客户端的授权范围
                .authorizedGrantTypes("client_credentials")
                .accessTokenValiditySeconds(7 * 24 *3600) //token的有效期:1周
                .refreshTokenValiditySeconds(30 * 24 * 3600) //refresh_token的有效期: 30天
                .and()
                //管理系统
                .withClient("admin") //第三方客户端的名称
                .secret(passwordEncoder.encode("admin"))//第三方客户端的密钥
                .scopes("all") //第三方客户端的授权范围
                .authorizedGrantTypes("password", "refresh_token")
                .accessTokenValiditySeconds(7 * 24 *3600) //token的有效期:1周
                .refreshTokenValiditySeconds(30 * 24 * 3600) //refresh_token的有效期: 30天
                .and()
                //app
                //解决受保护资源的Token的传递问题
                .withClient("app")
                .authorizedGrantTypes("password", "refresh_token")
                .secret(passwordEncoder.encode("app"))
                .authorizedGrantTypes("client_credentials") // 授权方式
                .scopes("all")
                .accessTokenValiditySeconds(7 * 24 * 3600); // 1周
        super.configure(clients);
    }

    /**
     * 配置验证管理器 {@link AuthenticationManager}和{@link UserDetailsService}
     * @param endpoints
     **/
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService)
                .tokenStore(jwtTokenStore())
                .tokenEnhancer(jwtAccessTokenConverter());
        super.configure(endpoints);
    }

    private TokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    private JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();

        KeyPair keyPair =  RSAUtil.getKeyPair();

        jwtAccessTokenConverter.setKeyPair(keyPair);



        return jwtAccessTokenConverter;
    }

}
